package socket;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;

/**
 * @program JavaBase
 * @description:
 * @author: zhanglu
 * @create: 2019-06-12 08:39:00
 */
public class TestRunTime01 {

    @Test
    public void test01() throws Exception {
        Process process = Runtime.getRuntime().exec("ifconfig");
        process.waitFor();
        InputStream in = process.getInputStream();
        InputStreamReader inr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(inr);
        String res = "";
        String str = "";
        while((str=br.readLine()) != null){
            res += str + "\r\n";
        }
        System.out.println(res);
    }

    @Test
    public void test02() throws Exception {
        InetAddress host = InetAddress.getLocalHost();
        String ip =host.getHostAddress();
        System.out.println("本机ip地址：" + ip);
        Process p = Runtime.getRuntime().exec("ping " + ip);
        p.wait(5000);
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            if (line.length() != 0)
                sb.append(line + "\r\n");
        }
        System.out.println("本次指令返回的消息是：");
        System.out.println(sb.toString());
    }

}
